package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import com.kdtech.analyse.common.AnalyseSelector;
import com.kdtech.analyse.common.NewsAnalyseConfig;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.StringUtils;

/**
 * 南海网解析
 * @author Persh
 *
 */
public class HiNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.hinews.cn/(?!bbs|shipin).*/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
		"http://auto.hinews.cn/contents/.*/[0-9]{8}/[0-9]*.shtml",
		"http://.*.hinews.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml"};
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsAnalyseConfig config=new NewsAnalyseConfig("南海网");
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}	
		
		if(htmltxt.contains("window.location")){
			String swichUrl=StringUtils.substringBetween(htmltxt, "window.location", ";");
			swichUrl=StringUtils.substringBetween(swichUrl, "\"", "\"");
			urlMeta=CrawlHTML.responseToURL(swichUrl);
			htmltxt=urlMeta.getHtml();
		}

		//标题解析规则
		config.getTitleRegexs().add(AnalyseSelector.init().select("title").substringBefore("_").substringBefore("-"));	
		
		//正文解析规则
		config.getContentRegexs().add(AnalyseSelector.init().select("td#content"));	
		config.getContentRegexs().add(AnalyseSelector.init().select("div#contents"));		
		config.getContentRegexs().add(AnalyseSelector.init().select("div.pb"));		
		config.getContentRegexs().add(AnalyseSelector.init().select("div.w600"));		
		config.getContentRegexs().add(AnalyseSelector.init().select("div.w675"));		
		config.getContentRegexs().add(AnalyseSelector.init().select("div.w620"));		
		
		//日期解析规则
		config.getDateRegexs().add(AnalyseSelector.init().select(("div#laiy")));		
		config.getDateRegexs().add(AnalyseSelector.init().select(("div.f12")));
		config.getDateRegexs().add(AnalyseSelector.init().select(("div.cGray")));
		config.getDateRegexs().add(AnalyseSelector.init().select(("div.llink0003")));
					
		config.getAuthorRegexs().add(AnalyseSelector.init().substringBetween("来源：","作者："));
		config.getAuthorRegexs().add(AnalyseSelector.init().substringBetween("来源：","||"));
		config.getAuthorRegexs().add(AnalyseSelector.init().substringBetween("来源："," "));
		config.getAuthorRegexs().add(AnalyseSelector.init().select("div#laiy").substringBetween("来源：","浏览次数"));
		config.getAuthorRegexs().add(AnalyseSelector.init().select("div.llink0003").substringBetween("来源：","　"));
		return null;//AbstractCommNewsAnalyse.parseHtmlWithConfig(urlMeta, config);	
	}
	
	public static void main(String[] args) {
		HiNewsAnalyse analyse=new HiNewsAnalyse();
		String url="http://sanya.hinews.cn/system/2014/03/10/016513260.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.isDetailPage(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url=meta.getUrl();
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		if(urlMeta!=null){
			return parserHtml(urlMeta);
		}
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
